package com.party.business.controller;


import java.util.Arrays;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.party.business.domain.TQuestion;
import com.party.business.domain.TQuestionDxDTO;
import com.party.business.domain.TQuestionOption;
import com.party.business.domain.TQuestionPdDTO;
import com.party.business.service.ITQuestionService;
import com.party.common.annotation.Log;
import com.party.common.core.controller.BaseController;
import com.party.common.core.domain.AjaxResult;
import com.party.common.core.domain.entity.SysUser;
import com.party.common.core.page.TableDataInfo;
import com.party.common.enums.BusinessType;
import com.party.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;


/**
 * 试题信息Controller
 *
 * @author ruoyi
 * @date 2024-11-08
 */
@RestController
@RequestMapping("/business/question" )
public class TQuestionController extends BaseController {
    @Autowired
    private ITQuestionService tQuestionService;

/**
 * 查询试题信息列表
 */
//@RequiresPermissions("system:question:list")
@GetMapping("/list" )
    public TableDataInfo list(TQuestion tQuestion) {
        startPage();

        List<TQuestion> list = tQuestionService.selectTQuestionList(tQuestion);
        return getDataTable(list);
    }

    /**
     * 导出试题信息列表
     */
    //@RequiresPermissions("system:question:export")
    @Log(title = "试题信息" , businessType = BusinessType.EXPORT)
    @PostMapping("/export" )
    public void export(HttpServletResponse response, TQuestion tQuestion) {
        List<TQuestion> list = tQuestionService.selectTQuestionList(tQuestion);
        ExcelUtil<TQuestion> util = new ExcelUtil<TQuestion>(TQuestion. class);
        util.exportExcel(response, list, "试题信息数据" );
    }

    /**
     * 获取试题信息详细信息
     */
    //@RequiresPermissions("system:question:query")
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) String id) {
        return AjaxResult.success(tQuestionService.selectTQuestionById(id));
    }

    /**
     * 新增试题信息
     */
    //@RequiresPermissions("system:question:add")
    @Log(title = "试题信息" , businessType = BusinessType.INSERT)
    @PostMapping
    @Transactional
    public AjaxResult add(@RequestBody TQuestion tQuestion) {
        tQuestionService.saveQuestion(tQuestion);
        return success();
    }

    /**
     * 修改试题信息
     */
    //@RequiresPermissions("system:question:edit")
    @Log(title = "试题信息" , businessType = BusinessType.UPDATE)
    @PutMapping
    @Transactional
    public AjaxResult edit(@RequestBody TQuestion tQuestion) {
         tQuestionService.updateQuestion(tQuestion);
        return success();
    }

    /**
     * 删除试题信息
     */
    //@RequiresPermissions("system:question:remove")
    @Log(title = "试题信息" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(tQuestionService.removeByIds(Arrays.asList(ids)));
    }

    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<TQuestionDxDTO> util = new ExcelUtil<TQuestionDxDTO>(TQuestionDxDTO.class);
        List<TQuestionDxDTO> quList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = tQuestionService.importQuestion(quList, operName);
        return success(message);
    }

    @PostMapping("/importDataPd")
    public AjaxResult importDataPd(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<TQuestionPdDTO> util = new ExcelUtil<TQuestionPdDTO>(TQuestionPdDTO.class);
        List<TQuestionPdDTO> quList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = tQuestionService.importPdQuestion(quList, operName);
        return success(message);
    }



    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<TQuestionDxDTO> util = new ExcelUtil<TQuestionDxDTO>(TQuestionDxDTO.class);
        util.importTemplateExcel(response, "选择题导入模版");
    }

    @PostMapping("/importTemplatePd")
    public void importTemplatePd(HttpServletResponse response)
    {
        ExcelUtil<TQuestionPdDTO> util = new ExcelUtil<TQuestionPdDTO>(TQuestionPdDTO.class);
        util.importTemplateExcel(response, "判断题导入模版");
    }

}
